import { computed, onMounted, ref, watch } from 'vue';
import { toLine, deepClone } from '@/utils/util';
import http from '@/utils/request';
import api from '@/api';
export default function initSelect(props, value) {
	let pageSize = 20;
	let queryParam = '';
	const nodeLabel = computed(() => {
		if (props.defaultFields.bindLabel) {
			return props.defaultFields.bindLabel;
		}
		return props.defaultFields.label;
	});

	const nodeValue = computed(() => {
		if (props.defaultFields.bindValue) {
			return props.defaultFields.bindValue;
		}
		return props.defaultFields.value;
	});

	const showLabel = computed(() => {
		if (props.defaultFields.showLabel) {
			if (typeof props.defaultFields.showLabel === 'string') {
				return [props.defaultFields.showLabel];
			} else {
				return props.defaultFields.showLabel;
			}
		}
		return [nodeLabel.value];
	});

	let theOptions = ref([]);
	watch(
		() => props.options,
		(val) => {
			theOptions.value = val;
		},
		{
			immediate: true,
		}
	);

	function userSearch(url: string | undefined | null, searchParams: object | undefined | null) {
		let currentUrl;
		if (url) {
			currentUrl = url;
		} else {
			currentUrl = api.common.getDictSearchData.url;
		}
		return http.post(currentUrl, searchParams);
	}

	function scrollMethod() {
		if (props.remote) {
			pageSize += 10;
			remoteMethod(value.value);
		}
	}

	function remoteMethod(query: string) {
		let params = deepClone(props.searchParams);
		params.pageNum = 1;
		params.pageSize = pageSize;
		if (!params.tableName) {
			params.tableName = 'sys_dict';
		}
		let searchField = props.searchField;
		if (!searchField) {
			searchField = toLine(nodeLabel.value);
		}

		params.keyName = nodeLabel.value;
		params.valueName = nodeValue.value;
		params.searchField = searchField;
		if (query) {
			params.searchText = query;
			params[searchField] = query;
		}
		let searchUrl = '';
		if (props.funcCode) {
			searchUrl = api.common.send.url + props.funcCode;
		} else {
			searchUrl = props.searchUrl;
		}
		userSearch(searchUrl, params).then((res: any) => {
			theOptions.value = res.result.list;
		});
	}
	function initSelectMethod(multiple) {
		let params = deepClone(props.searchParams);
		params.pageNum = 1;
		params.pageSize = 20;
		if (!params.tableName) {
			params.tableName = 'sys_dict';
		}
		let searchField = props.searchField;
		if (!searchField) {
			searchField = toLine(nodeValue.value);
		}
		if (props.modelValue) {
			if (typeof props.modelValue === 'number' || typeof props.modelValue === 'boolean' || props.modelValue.length > 0) {
				params.queryList.push({
					fieldName: toLine(nodeValue.value),
					fieldValue: props.modelValue,
				});
			}
		}

		if (multiple) {
			let idList = null;
			if (props.modelValue) {
				idList = props.modelValue;
			}
			params[nodeValue.value + 'List'] = idList;
		} else {
			params[nodeValue.value] = props.modelValue;
		}

		params.keyName = nodeLabel.value;
		params.valueName = nodeValue.value;
		params.searchField = searchField;
		let searchUrl = '';
		if (props.funcCode) {
			searchUrl = api.common.send.url + props.funcCode;
		} else {
			searchUrl = props.searchUrl;
		}
		userSearch(searchUrl, params).then((res: any) => {
			theOptions.value = res.result.list;
		});
	}

	function blurMethod(e) {
		if (e) {
			value.value = e.target.value;
		}
	}
	return {
		theOptions,
		nodeLabel,
		nodeValue,
		showLabel,
		scrollMethod,
		remoteMethod,
		initSelectMethod,
		blurMethod,
	};
}
